package com.wing.productionPlan.service;

import cn.hutool.core.date.DateUtil;
import com.wing.common.utils.DateUtils;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.wechat.WechatCheckinService;
import com.wing.wechat.WechatDepartmentService;
import com.wing.wechat.WechatUserService;
import com.wing.wechat.util.WechatUtil;
import com.wing.wechat.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生产排班-ServiceImpl
 *
 * @author xiayang
 */
@Service
@Slf4j
public class ProductionScheduleServiceImpl implements ProductionScheduleService {

    @Autowired
    private WechatDepartmentService departmentService;

    @Autowired
    private WechatUserService userService;

    @Autowired
    private WechatCheckinService checkinService;

    @Override
    public ResponseDepartmentList departmentList(Integer departmentId) {
        ResponseDepartmentList departmentList = departmentService.departmentList(departmentId);
        return departmentList;
    }

    @Override
    public ResponseUpdateDepartment updateDepartment(RequestUpdateDepartment updateDepartment) {
        ResponseUpdateDepartment responseUpdateDepartment = departmentService.updateDepartment(updateDepartment);
        return responseUpdateDepartment;
    }

    @Override
    public ResponseDeleteDepartment deleteDepartment(Integer departmentId) {
        ResponseDeleteDepartment responseDeleteDepartment = departmentService.delDepartment(departmentId);
        return responseDeleteDepartment;
    }

    @Override
    public ResponseDepartmentUserList departmentUserList(Integer departmentId) {
        ResponseToken token = userService.getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        ResponseDepartmentUserList departmentUserList = userService.departmentUserList(token.getAccess_token(), departmentId);
        return departmentUserList;
    }

    @Override
    public ResponseUpdateUser updateUser(RequestUpdateUser updateUser) {
        ResponseUpdateUser responseUpdateUser = userService.updateUser(updateUser);
        return responseUpdateUser;
    }

    @Override
    public ResponseDeleteUser deleteUser(String userId) {
        ResponseDeleteUser deleteUser = userService.delUser(userId);
        return deleteUser;
    }

    @Override
    public ResponseCheckinSchedule scheduleList(String queryTime, String userId) {
        RequestCheckinSchedule checkinSchedule = new RequestCheckinSchedule();
        if (StringUtils.isNotBlank(queryTime)) {
            checkinSchedule.setStarttime(DateUtils.timeStampToUint32(DateUtils.getStartOfMonth(DateUtils.stringFormatDate(queryTime, DateUtils.DATE_PATTERN_FORMAT_MONTH)).getTime()));
            checkinSchedule.setEndtime(DateUtils.timeStampToUint32(DateUtils.getStartOfMonth(DateUtils.stringFormatDate(queryTime, DateUtils.DATE_PATTERN_FORMAT_MONTH)).getTime()));
        } else {
            checkinSchedule.setStarttime(DateUtils.timeStampToUint32(DateUtils.getStartOfMonth(new Date()).getTime()));
            checkinSchedule.setEndtime(DateUtils.timeStampToUint32(DateUtils.getEndOfMonth(new Date()).getTime()));
        }
        List<String> userList = new ArrayList<>();
        userList.add(userId);
        checkinSchedule.setUseridlist(userList);
        ResponseCheckinSchedule responseCheckinSchedule = checkinService.getCheckinScheduleList(checkinSchedule);
        return responseCheckinSchedule;
    }
}
